package com.tomas.demo.common.utils;


import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
	public static String datefmt = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前时间字符串
     * @param format 输出格式 默认yyyy-MM-dd hh:mm:ss
     * @return
     */
	public static String getCurTimeStr(String... format){
        if(null!=format){
            datefmt=format[0];
        }

        String _date = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(datefmt);
            _date = df.format(new Date(System.currentTimeMillis()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return _date;
	}

    /**
     * 指定时间转字符串
     * @param t
     * @param format
     * @return
     */
	public static String fmtDate(Date t,String... format){
        if(null!=format){
            datefmt=format[0];
        }
        String _date = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(datefmt);
            _date = df.format(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return _date;
	}

    /**
     * 字符串转时间 格式:yyyy-MM-dd hh:mm:ss
     * @param datestr
     * @return
     */
	public static  Date parseDate(String datestr){
		SimpleDateFormat sdf = new SimpleDateFormat(datefmt);  
	    try{Date date = sdf.parse(datestr);return date;}catch(Exception e){}
	    return null;
	}
    /**
     * 两个时间相隔天数 time1-time2
     * @param time1
     * @param time2
     * @return
     */
    public static long daysBetweenDate(Date time1, Date time2){
        if (time1 == null || time2 == null) {
            return 0;
        }
        return (time1.getTime() - time2.getTime()) / 1000 / 60 / 60 / 24;
    }

    /**
     * 时间之间相隔小时数
     * @param date1
     * @param date2
     * @return
     */
	public static int hoursBetweenDate(Date date1, Date date2){
		if (null == date1 || null == date2) {
           return -1;
		}
		long intervalMilli = date2.getTime() - date1.getTime();
		return (int) (intervalMilli / ( 60 * 60 * 1000));  
	}

    /**
     * 时间之间相隔分钟数
     * @param date1
     * @param date2
     * @return
     */
	public static int minutesBetweenDate(Date date1, Date date2){
		if (null == date1 || null == date2) {
           return -1;
		}
		long intervalMilli = date2.getTime() - date1.getTime();
		return (int) (intervalMilli / ( 60 * 1000));  
	}
}

